package service

import (
	"gin/TEST01/entity"
	"gin/common"
	"gin/common/constant"
	"gin/common/db/config"
	util2 "gin/common/util"
	sysEntity "gin/system/entity"
	"github.com/gin-gonic/gin"
	"log"
)

//用户权限处理
func SysPermissionService(c *gin.Context, sysUserEntity *sysEntity.SysUserEntity) map[string]string {
	var resultList map[string]string
	//用户账号
	userName := sysUserEntity.UserName
	//超级管理员 admin  TODO 未处理  用户角色中包含 ”admin“ 的用户
	if userName == "admin" {
		resultList["admin"] = "*:*:*"
	}

	//非超级管理员用户

	//查询用户 sys_role_menu， sys_user_role， sys_role
	err := config.GormDb.Model(&entity.SysUserEntity{}).Table("sys_menu as m").Select("distinct m.perms").
		Joins("LEFT JOIN sys_role_menu rm on m.menu_id = rm.menu_id").
		Joins("LEFT JOIN sys_user_role ur on rm.role_id = ur.role_id").
		Joins("LEFT JOIN sys_role r on r.role_id = ur.role_id").
		Where("m.status = '0' and r.status = '0' and ur.user_id = ?", userName).Scan(&resultList).Error

	if err != nil {
		log.Println("用户权限信息查询失败，原因：", err)
		errToJson := util2.MapToJson(err)
		common.ReturnError(c, constant.ERRORCODE(), errToJson, resultList, int64(len(resultList)))
		panic("")
	}

	return resultList

}
